package com.ruoyi.web.web.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.Account;
import com.ruoyi.accountant.domain.Currency;
import com.ruoyi.accountant.domain.other.CallResult;
import com.ruoyi.accountant.service.AccountService;
import com.ruoyi.accountant.service.CurrencyService;
import com.ruoyi.accountant.service.ReasonService;
import com.ruoyi.web.web.dto.MessageDto;
import com.ruoyi.web.web.form.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/***
 * 账户相关操作
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private CurrencyService currencyService;

    @Autowired
    private ReasonService reasonService;

    @PostMapping("/add")
    public String add(AccountForm form) {
        Account account = new Account();

        Currency currency = new Currency(form.getMainCurrency());
        account.setMainCurrencyObject(currency);
        BeanUtils.copyProperties(form, account);

        accountService.save(account);
        return "redirect:/pages/item/account.html";
    }

    @PostMapping("/update")
    public String update(AccountForm form) {
        System.out.println("updata-------------------------");
        Account byId = accountService.findById(form.getAid());
        byId.setAname(form.getAname());
        byId.setAcode(form.getAcode());
        byId.setAtype(form.getAtype());
        byId.setMainCurrency(form.getMainCurrency());
        accountService.update(byId);
        return "redirect:/pages/item/account.html";
    }

    /***
     * 停用账户
     * @param aid
     * @return
     */
    @GetMapping("/stop")
    public String stop(Long aid) {
        System.out.println("stop-------------------");
        Account byId = accountService.findById(aid);
        byId.setStop(AppConf.ON);
        accountService.update(byId);
        return "redirect:/pages/item/account.html";
    }

    /***
     * 回复账户的使用
     * @param aid
     * @return
     */
    @GetMapping("/reuse")
    public String reuse(Long aid) {
        Account byId = accountService.findById(aid);
        byId.setStop(AppConf.OFF);
        accountService.update(byId);
        return "redirect:/pages/item/account.html";
    }

    @GetMapping("/delete")
    public String delete(Long aid) {
       if (accountService.deleteById(aid)>=1){

       };
        return "redirect:/pages/item/account.html";
    }

    /***
     * 账户收入
     * @return
     */
    @PostMapping("/income")
    public AjaxResult income(@RequestBody IncomeForm accountForm, HttpSession session) {
        System.out.println(accountForm.toString());
        Validation validate = accountForm.validate(false, accountService, currencyService, reasonService, session);
        if(validate.getOk()) {
            return AjaxResult.success(accountService.income(accountForm));
        } else {
            return AjaxResult.error();
        }

    }

    /***
     * 账户支出
     * @return
     */
    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody PayForm payForm, HttpSession session) {
        Validation validate = payForm.validate(false, accountService,currencyService,reasonService, session);
        if(validate.getOk()) {
            return AjaxResult.success(accountService.pay(payForm));
        } else {
            return AjaxResult.error();
        }
    }

    @PostMapping("/transfer")
    public MessageDto transfer(@RequestBody TransferForm transferForm, Model model) {
        Validation validate = transferForm.validate(false, accountService, currencyService);
        if(validate.getOk()) {
            accountService.transfer(transferForm);
        } else {
            return new MessageDto(400, validate.getMessage());
        }
        model.addAttribute("transferForm", transferForm);
        return MessageDto.ok();
    }
}